package com.z.admin.controller;

import com.github.pagehelper.PageInfo;
import com.z.base.vo.PageVo;
import com.z.base.vo.Result;
import com.z.base.web.BaseController;
import com.z.blog.entity.Tag;
import com.z.blog.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

import static com.z.base.vo.Result.decide;

/**
 * 博客标签Controller
 *
 * @author z
 * @date 2022-09-09
 */
@RestController
@RequestMapping("/admin/tag")
public class AdminTagController extends BaseController {

    private String prefix = "admin/tag";

    @Autowired
    private ITagService tagService;

    @GetMapping("/main")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询博客标签列表
     */
    @ResponseBody
    @GetMapping("/data")
    public Result list(@ModelAttribute Tag tag, PageVo pageVo) {
        PageInfo<Tag> pageInfo = tagService.selectTagPage(tag, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增博客标签
     */
    @GetMapping("/add")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增博客标签
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody Tag tag) {
        return decide(tagService.save(tag));
    }

    /**
     * 修改博客标签
     */
    @GetMapping("/edit")
    public ModelAndView edit(Long id, ModelMap map) {
        Tag tag =tagService.getById(id);
        map.put("tag", tag);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改博客标签
     */
    @ResponseBody
    @PutMapping("/update")
    public Result update(@RequestBody Tag tag) {
        return decide(tagService.updateById(tag));
    }

    /**
     * 删除博客标签
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    public Result batchRemove(String ids) {
        return decide(tagService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除博客标签
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        return decide(tagService.removeById(id));
    }
}
